package com.hanlink.dsp.ui.widget.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.hanlink.dsp.util.EqUtil;
import com.qztech.sdk.R;
import com.qztech.utils.YzDebug;

import java.util.Arrays;

/**
 * <pre>
 *
 *  BTDSP
 *
 *
 *  Copyright (C): 2015/10/11
 *
 * Description:
 *
 * Revision History
 * Date			  Author		Description
 * 2015/10/11        PeterVon       初始创建
 *
 * </pre>
 */
public class EqGridView extends View {
    public static float EQ_TIMES_CAL_P     = -0.5555556F;
    public static float TEXT_SIZE = 24f;
//    protected static final int dbCount = 36;
    private static final short[] fcValue = new short[]{(short) 20, (short) 100, (short) 320, (short) 500, (short) 750, (short) 1250, (short) 3000, (short) 6000,(short) 10000 ,(short) 14000,(short) 22000};
    private static final String[] gridHz = new String[]{"20", "100", "320", "500", "750", "1.25k", "3k", "6k","10k","14k","22k"};
    public static String[] griddB = new String[]{"12", "6", "0dB", "-6", "-12"};
//    private static final String[] griddB = new String[]{"20", "15", "10", "5", "0dB", "-5", "-10", "-15", "-20"};
    private static Paint paintGreen = null;
    protected static Paint paintWhite = new Paint();
//    protected static final int sBottom = 40;
//    protected static final int sLeft = 60;
//    protected static final int sRight = 20;
//    protected static final int sTop = 20;
    protected int[] dbGrid;
    private int[] fcGrid;
    protected int mBottom;
    protected int mHeight;
    protected int mTop;
    protected int mWidth;
    protected float times;
    protected int baseColor = 0xFF0063a8;

    public EqGridView(Context context) {
        super(context);
        initView();
    }

    public EqGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public EqGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void initView() {
        this.dbGrid = new int[griddB.length];
        this.fcGrid = new int[fcValue.length];
//        this.setBackgroundColor(Color.parseColor("#989898"));
        this.InitPaint();
    }


    private void InitPaint() {
        if (paintGreen == null) {
            paintGreen = new Paint();
            paintGreen.setColor(baseColor);
            paintWhite.setColor(getContext().getResources().getColor(com.hanlink.res.R.color.white));
            paintWhite.setTextSize(TEXT_SIZE);
        }

    }

    private void drawGrid(Canvas canvas) {
        Rect var3 = new Rect();
        if (canvas.getWidth() >= 1 && canvas.getHeight() >= 1) {
            int var2;
            for (var2 = 0; var2 < this.dbGrid.length; ++var2) {
                paintWhite.getTextBounds(griddB[var2], 0, griddB[var2].length(), var3);
                canvas.drawText(griddB[var2], (float) (60 - var3.width() - 10), (float) (this.dbGrid[var2] + var3.height() / 2), paintWhite);
                canvas.drawLine(60.0F, (float) this.dbGrid[var2], (float) (this.mWidth - 20), (float) this.dbGrid[var2], paintGreen);
            }

            for (var2 = 0; var2 < this.fcGrid.length; ++var2) {
                paintWhite.getTextBounds(gridHz[var2], 0, gridHz[var2].length(), var3);
//                if (var2 != 8 && var2 != 17 && var2 != 26) {
//                    var1.drawLine((float) this.fcGrid[var2], (float) this.mTop, (float) this.fcGrid[var2], (float) this.mBottom, paintGreen);
//                    if (var2 == 0 || var2 == 28) {
//                        var1.drawText(gridHz[var2], (float) (this.fcGrid[var2] - var3.width() / 2), (float) (this.mBottom + var3.height() + 10), paintWhite);
//                    }
//                } else {
                    canvas.drawLine((float) this.fcGrid[var2], (float) this.mTop, (float) this.fcGrid[var2], (float) this.mBottom, paintGreen);
                    canvas.drawText(gridHz[var2], (float) (this.fcGrid[var2] - var3.width() / 2), (float) (this.mBottom + var3.height() + 10), paintWhite);
//                }
            }
        }
    }

    private void reGird(int width, int height) {
        int useHeight = height - 20 - 40;

        for (int i = 0; i < this.fcGrid.length; ++i) {
            this.fcGrid[i] = EqUtil.fc2int(width - 60 - 20, 0, fcValue[i]) + 60;
        }

        int space = useHeight / (this.dbGrid.length - 1);
        int start = dbGrid.length/2;
        this.dbGrid[start] = useHeight / 2 + 20;
        int before, after, value ;
        for (int i = 0, len = start+1; i < len; i++ ){
            before = start - i;
            if (before >= 0){
                value = this.dbGrid[start] - space*i;
                this.dbGrid[before] = value;
            }
            after = start + i;
            if (after < dbGrid.length){
                value = this.dbGrid[start] + space*i;
                this.dbGrid[after] = value;
            }

        }
        // this.dbGrid[4] = this.dbGrid[4] - var1 * 2;
    /*    this.dbGrid[3] = this.dbGrid[4] - space;
        this.dbGrid[2] = this.dbGrid[4] - space * 2;
        this.dbGrid[1] = this.dbGrid[4] - space * 3;
        this.dbGrid[0] = this.dbGrid[4] - space * 4;


        this.dbGrid[5] = this.dbGrid[4] + space;
        this.dbGrid[6] = this.dbGrid[4] + space * 2;
        this.dbGrid[7] = this.dbGrid[4] + space * 3;
        this.dbGrid[8] = this.dbGrid[4] + space * 4;*/
/*        this.dbGrid[9] = this.dbGrid[6] + var1 * 3;
        this.dbGrid[10] = this.dbGrid[6] + var1 * 4;
        this.dbGrid[11] = this.dbGrid[6] + var1 * 5;
        this.dbGrid[12] = this.dbGrid[6] + var1 * 6;*/
        this.mTop = this.dbGrid[0];
        this.mBottom = this.dbGrid[dbGrid.length-1];
        YzDebug.d("reGird: "+ Arrays.toString(dbGrid) +" w:"+width +" h:"+height +" uh:"+useHeight );
    }

    @Override
    protected void onDraw(Canvas var1) {
        super.onDraw(var1);
        this.drawGrid(var1);
    }

    @Override
    protected void onSizeChanged(int width, int height, int oldw, int oldh) {
        super.onSizeChanged(width, height, oldw, oldh);
        if (width >= 1 && height >= 1) {
            this.times = EQ_TIMES_CAL_P * (float) (height - 20 - 40);
            this.reGird(width, height);
            this.mWidth = width;
            this.mHeight = height;
        }

    }
}
